from market.models import Asset
from market.models import Player
from market.models import Strategy
from market.models import History
from market.models import AssetForHistory
from market.models import StrategyForHistory
from market.models import PlayerPortfolioForHistory
#from market.marketData import MarketData
#from market.playerData import PlayerData
from market.view import View


# create a new history
# we pass the strategy which is changed. This allow to set its performance to 0 accross the transaction
def createHistory( marketData, strategy ):
    
    hl = [ hh for hh in History.objects.all()]
    if len( hl )  > 0 :
            previousHisto = hl[len( hl ) - 1]
    h = History()
    h.save()
    mktView = marketData.getMarketView()

    #ASSETS
    for a in Asset.objects.all():
        afh = AssetForHistory( asset = a,
                               history = h,
                               price = mktView.getViewOnAsset( a ) )
        afh.save()

    #STRATEGIES
    for s in Strategy.objects.all():
        sv = View()
        sv.setViewFromStrategy( s )
        mtm = marketData.getStrategyMtM( sv )
        np = 0
        na = 0
        for p in Player.objects.all():
            for nais in p.numberofassetsandmtmpercentageinstrategy_set.all():
                if nais.strategy == s:
                    np = np + 1
                    na = na + nais.numberOfAssetsInStrategy
        # IMPLEMENT strategyPerfSinceLastHistoryDate:
        # If there is no previous history: 0
        # If there is a previous history date
        #   in case it is not the changing strategy: mtm before / mtm - 1
        #   in case of the changing strategy: 0
        perf = 0
        if len( hl )  > 0 and s.name != strategy.name:#there is a prvious history and it is not the changed strategy
            for ss in previousHisto.strategyforhistory_set.all():
                if ss.strategy.name == s.name:# we get the strategy at the previous history date.
                    if( ss.strategyMtM != 0 ):
                        perf = mtm / ss.strategyMtM - 1
        sfh = StrategyForHistory(
            strategy = s,
            history = h,
            strategyMtM = mtm,
            strategyPerfSinceLastHistoryDate = perf,
            numberOfPlayersInvestedInTheStrategy = np,
            totalNumberOfAssetInvestedInTheStrategy = na )
        sfh.save()

    #PORTFOLIOSS
    for p in Player.objects.all():
        mtm = 0
        nbStrats = 0
        nbAssets = 0
        percInMain = 0
        for s in p.numberofassetsandmtmpercentageinstrategy_set.all():
                pv = View()
                pv.setViewFromStrategy( s.strategy )
                tmp = s.numberOfAssetsInStrategy
                mtm = mtm + tmp * marketData.getStrategyMtM( pv )
                nbStrats = nbStrats + 1
                nbAssets = nbAssets + tmp
                if s.strategy.name == p.mainStrategy:
                    percInMain = tmp
        percInMain = percInMain / nbAssets
        pfh = PlayerPortfolioForHistory(
            player = p,
            history = h,
            portfolioMtM = mtm,
            investementInMainStrategyPercentage = percInMain,
            numberOfStrategies = nbStrats )
        pfh.save()
        
    return
        
